using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VAMVALUE
    {
        public void SetUpNewRow(ref DataRow dwReturn)
        {
            #region
            #endregion
            switch (app.company)
            {
                case "YGR":
                case "SXN":
                case "TST":
                case "UBT":
                default:
                    #region
                    dwReturn["AMVMANDANT"] = "00";
                    dwReturn["AMVNRKRS"] = "  ";
                    dwReturn["AMVLFDNR"] = "0000";
                    dwReturn["AMVPOS"] = "00";
                    dwReturn["AMVDATE"] = "0";
                    dwReturn["AMVTIME"] = "0";
                    dwReturn["AMVWERTNR"] = "1";
                    dwReturn["AMVVAL"] = "0";
                    dwReturn["AMVMTR"] = "0";
                    #endregion
                    break;
            }
        }
        public void InsertAMVALUE(DataRow dwData)
        {

            or1 = OracleDB.CreateCommand();
            switch (app.company)
            {
                case "YGR":
                case "SXN":
                case "TST":
                case "UBT":
                default:
                    or1.CommandText = "INSERT INTO AMVALUE ( AMVMANDANT,AMVNRKRS,AMVLFDNR,AMVPOS,AMVDATE,AMVTIME,AMVWERTNR,AMVVAL,AMVMTR)"
                   + " VALUES (:AMVMANDANT:,:AMVNRKRS:,:AMVLFDNR:,:AMVPOS:,:AMVDATE:,:AMVTIME:,:AMVWERTNR:,:AMVVAL:,:AMVMTR:)";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
            }

            Connect2Proxy(or1);
        }
        public override string BATCHINSERT(DataRow dwData)
        {
            string value = "";
            string sql = "";
            switch (app.company)
            {
                default:
                    sql = "INTO AMVALUE"
                    + " VALUES (:AMVMANDANT:,:AMVNRKRS:,:AMVLFDNR:,:AMVPOS:,:AMVDATE:,:AMVTIME:,:AMVWERTNR:,:AMVVAL:,:AMVMTR:)";
                    value = INTEXDBC.InjectData(sql, dwData);
                    break;
            }

            return value;
        }
        public void BULKINSERT(DataRow dwData, string bdeken)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO AMVALUE"
                       + " SELECT DISTINCT AMEMANDANT, AMENRKRS, AMELFDNR, AMEPOS,:AMVDATE:,:AMVTIME:,SUBSTR(AMEBDEKEN, 6, 3),:AMVVAL:,:AMVMTR:"
                       + "  FROM AMEINST JOIN AMSTAMM ON AMEMANDANT = AMMANDANT AND AMENRKRS = AMNRKRS AND AMELFDNR = AMLFDNR AND AMEPOS = AMPOS AND AMART <> 'ZLAN    '"
                       + " WHERE AMEBDEKEN = (SELECT AMEBDEKEN FROM AMEINST WHERE AMENRKRS = :AMVNRKRS: AND AMELFDNR = :AMVLFDNR: AND AMEPOS=:AMVPOS: AND SUBSTR(AMEBDEKEN,6, 3)= :AMVWERTNR:)";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
            or1.CommandText = " SELECT DISTINCT M.* FROM  ABARTE M "
                + " JOIN  AMEINST E ON ABEMANDANT = AMEMANDANT AND AMEMODNR = ABEMODNR AND AMEEINSTNR = ABEEINSTNR"
                + " JOIN ABFOLGE F ON ABFAUFKRS = ABEAUFKRS AND ABFAUFLFD = ABEAUFLFD AND ABFAUFPOS = ABEAUFPOS AND AMEMANDANT = ABFMANDANT AND ABFIMNRKRS = AMENRKRS AND ABFIMLFDNR = AMELFDNR AND ABFIMPOS = AMEPOS AND ABFSTATUS = ' '"
                + " WHERE AMEMANDANT=:AMVMANDANT: AND AMEBDEKEN='" + bdeken + "'";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                double value = System.Convert.ToDouble(dwData["AMVVAL"].ToString());
                row["ABEIWERTX"] = value.ToString().PadRight(16);
                row["ABEIWERTN"] = Helper.Left("0000000000000000" + (value * 1000000).ToString(), 16);
                VABE.UABE01(row);
            }
        }
        public DataSet QAMVALUE01(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT * FROM AMVALUE"
                            + " WHERE AMVMANDANT=:AMVMANDANT: AND AMVNRKRS=:AMVNRKRS: AND AMVLFDNR=:AMVLFDNR: AND AMVPOS=:AMVPOS: AND AMVDATE=:AMVDATE: AND AMVTIME=:AMVTIME: AND AMVWERTNR=:AMVWERTNR:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public DataSet QAMVALUE02(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT * FROM AMVALUE"
                            + " WHERE AMVMANDANT=:AMVMANDANT: AND AMVNRKRS=:AMVNRKRS: AND AMVLFDNR=:AMVLFDNR: AND AMVPOS=:AMVPOS: AND AMVDATE||AMVTIME>=:AMVDATE: AND AMVDATE||AMVTIME<=:AMVTIME:"
                            + " ORDER BY AMVNRKRS,AMVLFDNR,AMVPOS,AMVDATE,AMVTIME,AMVWERTNR";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);

            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public void DAMVALUE01(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE FROM AMVALUE "
                        + " WHERE AMVMANDANT=:AMVMANDANT: AND AMVNRKRS=:AMVNRKRS: AND AMVLFDNR=:AMVLFDNR: AND AMVPOS=:AMVPOS: AND AMVDATE=:AMVDATE: AND AMVTIME=:AMVTIME: AND AMVWERTNR=:AMVWERTNR:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }

        public void UpdateAMVALUE(DataRow dwData, int option = 1)
        {
            if (option == 0)
            {
                switch (app.company)
                {
                    case "YGR":
                    case "SXN":
                    case "TST":
                    case "UBT":
                    default:
                        UAMVALUE01(dwData);
                        break;
                }
            }
        }
        public void UAMVALUE01(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE AMVALUE SET AMVMANDANT=:AMVMANDANT:,AMVNRKRS=:AMVNRKRS:,AMVLFDNR=:AMVLFDNR:,AMVPOS=:AMVPOS:,AMVDATE=:AMVDATE:,AMVTIME=:AMVTIME:,AMVWERTNR=:AMVWERTNR:,AMVVAL=:AMVVAL:,AMVMTR=:AMVMTR:"
                + " WHERE AMVMANDANT=:AMVMANDANT: AND AMVNRKRS=:AMVNRKRS: AND AMVLFDNR=:AMVLFDNR: AND AMVPOS=:AMVPOS: AND AMVDATE=:AMVDATE: AND AMVTIME=:AMVTIME: AND AMVWERTNR=:AMVWERTNR:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
    }
}
